﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Infrastructure
{
    /// <summary>
    /// 唯一性校验
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = true)]
    public class EntityFieldCheckUniqueAttribute : Attribute
    {
        private bool isUnique = false;
        private string[] checkFields = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isUnique">是否唯一索引</param>
        /// <param name="isUniqueByOrg">同组织唯一</param>
        public EntityFieldCheckUniqueAttribute(bool isUnique, bool isUniqueByOrg)
            : base()
        {
            this.isUnique = isUnique;
            if (isUniqueByOrg)
            {
                this.checkFields = new string[] { "org_id" };
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="isUnique"></param>
        /// <param name="uniqueFields">分组字段</param>
        public EntityFieldCheckUniqueAttribute(bool isUnique, string[] uniqueFields)
         : base()
        {
            this.isUnique = isUnique;
            this.checkFields = uniqueFields;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isUnique">是否唯一索引</param>
        public EntityFieldCheckUniqueAttribute(bool isUnique)
            : base()
        {
            this.isUnique = isUnique;
        }

        /// <summary>
        /// 是否唯一索引
        /// </summary> 
        public bool IsUnique
        {
            get { return isUnique; }
        }

        /// <summary>
        ///  分组字段
        /// </summary> 
        public string[] UniqueFields
        {
            get { return checkFields; }
        }
    }
}
